package com.day240701.controller;

import com.day240701.dao.BlogDao;
import com.day240701.entity.Blog;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "BlogServlet", value = "/blog/manage")
public class BlogServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String op = request.getParameter("op");

        if("addBlog".equals(op)){
            addBlog(request,response);
        } else if("queryAllBlogs".equals(op)){
            queryAllBlogs(request,response);
        } else if("queryBlogById".equals(op)){
            queryBlogById(request,response);
        } else if("editBlog".equals(op)){
            editBlog(request,response);
        }
    }
    protected void addBlog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String title  = request.getParameter("title");
        String content  = request.getParameter("blogContent");

        BlogDao blogDao = new BlogDao();
        boolean flag = blogDao.addBlog(title, content);

        // 若添加成功
        if (flag) {
            response.sendRedirect("manage?op=queryAllBlogs");
        }else {
            HttpSession session = request.getSession();
            session.setAttribute("msg", "添加失败，请重新添加");
            response.sendRedirect("addBlog.jsp");
        }
    }
    protected void queryAllBlogs(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        BlogDao blogDao = new BlogDao();
        List<Blog> blogs = blogDao.queryAllBlogs();
        request.setAttribute("blogs", blogs);
        request.getRequestDispatcher("queryAllBlogs.jsp").forward(request,response);
    }
    protected void queryBlogById(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String blogIdString = request.getParameter("blogId");
        int blogId = Integer.parseInt(blogIdString);

        BlogDao blogDao = new BlogDao();
        Blog blog = blogDao.queryBlogById(blogId);

        request.setAttribute("blog", blog);
        request.getRequestDispatcher("queryBlogById.jsp").forward(request,response);
    }
    protected void editBlog(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 跳转到编辑界面
        String blogIdString = request.getParameter("blogId");
        int blogId = Integer.parseInt(blogIdString);

        BlogDao blogDao = new BlogDao();
        Blog blog =  blogDao.queryBlogById(blogId);
        request.setAttribute("blog",blog);

        request.getRequestDispatcher("editBlog.jsp").forward(request,response);
    }
}
